package com.songshuai.sales.tool.controller;

import com.songshuai.sales.tool.dto.request.service.type.QueryServiceTypeReqDto;
import com.songshuai.sales.tool.dto.request.service.type.ServiceTypeReqDto;
import com.songshuai.sales.tool.dto.response.service.type.ServiceTypeResDto;
import com.songshuai.sales.tool.model.base.YixueBaseServiceType;
import com.songshuai.sales.tool.service.YixueBaseServiceTypeService;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryRes;
import com.songshuai.webapi.common.annotation.RequestJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务标准化分类管理
 * @Author: wanglei
 * @Date: 2019/4/11 17:23
 */
@Slf4j
@Api("服务标准化分类管理")
@RestController
@RequestMapping("/api/serviceType/")
public class ServiceTypeController {

    @Autowired
    private YixueBaseServiceTypeService yixueBaseServiceTypeService;

    @ApiOperation(value = "查询分类列表")
    @PostMapping("typeList")
    public QueryRes queryServiceTypeList(@RequestBody QueryServiceTypeReqDto typeReqDto){
        try {
            List<ServiceTypeResDto> typeList = yixueBaseServiceTypeService.queryServiceTypeList(typeReqDto);
            return QueryRes.setResData(typeList);
        } catch (Exception e) {
            log.error("查询分类列表异常：{}",e);
            return new QueryRes(ResultStatus.ERROR_SEARCH_FAILED);
        }
    }

    @ApiOperation(value = "新增或修改服务分类")
    @PostMapping("saveOrUpdateType")
    public QueryRes saveOrUpdateServiceType(@RequestBody ServiceTypeReqDto type){
        try {
            Integer flag = yixueBaseServiceTypeService.saveOrUpdateServiceType(type);
            if(flag == 1){
                if(type.getId() == null){
                    return new QueryRes(ResultStatus.ERROR_CREATE_FAILED.getErrorCode(),"分类名已存在");
                }else{
                    return new QueryRes(ResultStatus.ERROR_UPDATE_FAILED.getErrorCode(),"分类名已存在");
                }
            }
        } catch (Exception e) {
            throw new BusinessException(ResultStatus.ERROR_CREATE_FAILED);
        }
        return new QueryRes();
    }

    @ApiOperation(value = "删除服务分类")
    @PostMapping("deleteType")
    public QueryRes deleteServiceType(@RequestJson("id") Integer id){
        try {
            Integer flag = yixueBaseServiceTypeService.deleteServiceType(id);
            if(flag == 1){
                return new QueryRes(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(),"分类不存在");
            }else if(flag == 2){
                return new QueryRes(ResultStatus.ERROR_DELETE_FAILED.getErrorCode(),"该分类下有子内容，不可删除");
            }
        } catch (Exception e) {
            throw new BusinessException(ResultStatus.ERROR_DELETE_FAILED);
        }
        return new QueryRes();
    }

}
